//
//  BXChagePwdVC.m
//  BXlive
//
//  Created by bxlive on 2019/7/26.
//  Copyright © 2019 cat. All rights reserved.
//

#import "BXChagePwdVC.h"
#import "BXGradientButton.h"
#import <FDFullscreenPopGesture/UINavigationController+FDFullscreenPopGesture.h>
#import <SLDeveloperTools/SLDeveloperTools.h>
#import <Masonry/Masonry.h>
#import "GradientButton.h"

@interface BXChagePwdVC ()

@property(nonatomic,strong)UIView *navigationView;
@property (nonatomic, strong) UITextField *oldTf;
@property (nonatomic, strong) UITextField *nowTf;
@property (nonatomic, strong) UITextField *confirmTf;

@end

@implementation BXChagePwdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.title = @"修改密码";
    self.fd_prefersNavigationBarHidden = YES;
    [self setupNavigationView];
    [self.view setBackgroundColor:[UIColor sl_colorWithHex:0xFFFFFF]];
    
    [self initViews];
}
-(void)setupNavigationView{
    _navigationView = [UIView new];
    [self.view addSubview:_navigationView];
    [_navigationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(__kTopAddHeight+64);
    }];
    [_navigationView setBackgroundColor:[UIColor sl_colorWithHex:0xFFFFFF]];
    WS(weakSelf);
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn addTarget:self action:@selector(pop) forControlEvents:BtnTouchUpInside];
    [_navigationView addSubview:backBtn];
    [backBtn setImage:CImage(@"back_black") forState:BtnNormal];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.mas_equalTo(0);
        make.width.height.mas_equalTo(44);
    }];
    UILabel *label = [UILabel createLabelWithFrame:CGRectZero BackgroundColor:SLClearColor Text:NSLocalizedString(@"修改密码", nil) Font:SLBFont(__ScaleWidth(18)) TextColor:sl_textColors];
    if ([[BXLiveUser currentBXLiveUser].isset_pwd intValue] == 0) {
        label.text = NSLocalizedString(@"设置密码", nil);
    }
    label.textAlignment = 1;
    [_navigationView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(44);
        make.right.mas_equalTo(-44); 
        make.centerY.equalTo(weakSelf.navigationView.mas_bottom).offset(-22);
        make.height.mas_equalTo(25);
    }];
}


- (void)initViews {
    
    NSArray *titleArr;
    BXLiveUser *user = [BXLiveUser currentBXLiveUser];
    if ([user.isset_pwd intValue] == 0) {
        titleArr = @[NSLocalizedString(@"请输入原密码", nil),@"",@""];
    }
    

    WS(weakSelf);
    UIView *oldV = [UIView new];
    oldV.layer.masksToBounds = YES;
    oldV.layer.cornerRadius = __ScaleWidth(24);
    oldV.backgroundColor = sl_subBGColors;
    [self.view addSubview:oldV];
    [oldV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(__ScaleWidth(12));
        make.right.mas_equalTo(__ScaleWidth(-12));
        make.top.equalTo(weakSelf.navigationView.mas_bottom).offset(__ScaleWidth(15));
        make.height.mas_equalTo(__ScaleWidth(48));
    }];
    _oldTf = [[UITextField alloc]init];
    _oldTf.font = SLPFFont(__ScaleWidth(14));
    _oldTf.textColor =sl_textColors;
    _oldTf.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"请输入原密码", nil) attributes:@{NSForegroundColorAttributeName:MinorColor}];
    _oldTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    _oldTf.secureTextEntry = YES;
    [oldV addSubview:_oldTf];
    UIButton *oldTfBtn =[_oldTf valueForKey:@"_clearButton"];
    [oldTfBtn setImage:CImage(@"login_clear") forState:BtnNormal];
    [_oldTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(__ScaleWidth(25));
        make.centerY.mas_equalTo(oldV);
        make.left.mas_equalTo(__ScaleWidth(20));
        make.right.mas_equalTo(__ScaleWidth(-20));
    }];
    
    
    UIView *nowV = [UIView new];
    nowV.layer.masksToBounds = YES;
    nowV.layer.cornerRadius = __ScaleWidth(24);
    nowV.backgroundColor = sl_subBGColors;
    [self.view addSubview:nowV];
    if ([user.isset_pwd intValue] != 0) {
        [nowV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(__ScaleWidth(12));
            make.right.mas_equalTo(__ScaleWidth(-12));
            make.top.equalTo(oldV.mas_bottom).offset(__ScaleWidth(15));
            make.height.mas_equalTo(__ScaleWidth(48));
        }];
    }else{
        oldV.hidden = YES;
        [nowV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(__ScaleWidth(12));
            make.right.mas_equalTo(__ScaleWidth(-12));
            make.top.equalTo(weakSelf.navigationView.mas_bottom).offset(__ScaleWidth(15));
            make.height.mas_equalTo(__ScaleWidth(48));
        }];
    }
    _nowTf = [[UITextField alloc]init];
    _nowTf.font = SLPFFont(__ScaleWidth(14));
    _nowTf.textColor =sl_textColors;;
    _nowTf.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"请输入新密码", nil) attributes:@{NSForegroundColorAttributeName:MinorColor}];
    _nowTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    _nowTf.secureTextEntry = YES;
    [nowV addSubview:_nowTf];
    UIButton *nowBtn =[_nowTf valueForKey:@"_clearButton"];
    [nowBtn setImage:CImage(@"login_clear") forState:BtnNormal];
    [_nowTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(__ScaleWidth(25));
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(__ScaleWidth(20));
        make.right.mas_equalTo(__ScaleWidth(-20));
    }];
    
    UIView *confirmV = [UIView new];
    confirmV.layer.masksToBounds = YES;
    confirmV.layer.cornerRadius = __ScaleWidth(24);
    confirmV.backgroundColor = sl_subBGColors;
    [self.view addSubview:confirmV];
    [confirmV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(__ScaleWidth(12));
        make.right.mas_equalTo(__ScaleWidth(-12));
        make.top.equalTo(nowV.mas_bottom).offset(__ScaleWidth(15));
        make.height.mas_equalTo(__ScaleWidth(48));
    }];
    _confirmTf = [[UITextField alloc]init];
    _confirmTf.font = SLPFFont(__ScaleWidth(14));
    _confirmTf.textColor = sl_textColors;
    _confirmTf.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"请确认新密码", nil) attributes:@{NSForegroundColorAttributeName:MinorColor}];
    _confirmTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    _confirmTf.secureTextEntry = YES;
    [confirmV addSubview:_confirmTf];
    UIButton *confirmBtn =[_nowTf valueForKey:@"_clearButton"];
    [confirmBtn setImage:CImage(@"login_clear") forState:BtnNormal];
    [_confirmTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(40);
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(__ScaleWidth(20));
        make.right.mas_equalTo(__ScaleWidth(-20));
    }];
    
//    UIButton *nextButton = [[UIButton alloc]init];
    UIButton *nextButton = [GradientButton new];
    nextButton.layer.masksToBounds = YES;
    nextButton.layer.cornerRadius = __ScaleWidth(22);
    [nextButton setTitle:NSLocalizedString(@"提交", nil) forState:BtnNormal];
    [nextButton sl_setTitleColor:sl_textColors];
    [nextButton setBackgroundColor:sl_subBGColors];
    nextButton.titleLabel.font = SLBFont(__ScaleWidth(16));
    [nextButton addTarget:self action:@selector(submitAction) forControlEvents:BtnTouchUpInside];
    [self.view addSubview:nextButton];
    [nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(nowV);
        make.top.mas_equalTo(confirmV.mas_bottom).offset(__ScaleWidth(50));
        make.height.mas_equalTo(__ScaleWidth(44));
    }];
    
    [_oldTf becomeFirstResponder];
}

- (void)submitAction {
    if ([[BXLiveUser currentBXLiveUser].isset_pwd integerValue]) {
        if (!_oldTf.text.length) {
            [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"请输入原密码", nil)];
            return;
        }
    }
    if (!_nowTf.text.length) {
        [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"请输入新密码", nil)];
        return;
    }
    if (_nowTf.text.length < 6) {
        [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"新密码少于6位", nil)];
        return;
    }
    
    if (!_confirmTf.text.length) {
        [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"请确认新密码", nil)];
        return;
    }
    if (![_confirmTf.text isEqualToString:_nowTf.text]) {
        [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"密码输入不一致", nil)];
        return;
    }
    [self.view endEditing:YES];
    [BGProgressHUD showLoadingWithMessage:nil];
    [NewHttpManager changePwdWithPassword:_nowTf.text old_password:_oldTf.text confirm_password:_confirmTf.text success:^(NSDictionary *jsonDic, BOOL flag, NSMutableArray *models) {
        [BGProgressHUD hidden];
        if (flag) {
            BXLiveUser *liveUser = [BXLiveUser currentBXLiveUser];
            liveUser.isset_pwd = @"1";
            [BXLiveUser setCurrentBXLiveUser:liveUser];
            
            [BGProgressHUD showInfoWithMessage:jsonDic[@"msg"]];
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [BGProgressHUD showInfoWithMessage:jsonDic[@"msg"]];
        }
    } failure:^(NSError *error) {
        [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"设置失败", nil)];
    }];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

@end
